<template>
	<view style="height: 100%" @click="gongSFalse">
		<view class="" v-if="!info_show">
			<ul class="db bgcf pd015" style="position: relative;">
				<li class="df ac sb h48 bbf1">
					<span class="f14 cf5">*</span>
					<p class="f14 c28">企业名称</p>
					<input class="tr f1 ml15 f14 c28" @input="onInputYY" :data-type="0" :value="val[0]"
						placeholder="名称与营业执照一致" placeholder-class="cc" />
					<view class="passed">
						<image @click.stop="gongS" v-if="!isPassed" style="width: 100%;height: 100%;"
							src="@/static/icon/not_passed.png" mode="widthFix"></image>
						<image @click.stop="gongS" v-else style="width: 100%;height: 100%;"
							src="@/static/icon/passed.png" mode="widthFix">
						</image>
						<view v-show="gongShow" class="pd_tab">
							<image v-if="isPassed" src="../../static/已通过.png" mode="aspectFit"></image>
							<image v-else src="../../static/未通过.png" mode="aspectFit"></image>
						</view>
					</view>
				</li>
				<view class="search-D" v-if="SySeach==true">
					<view @click="SySeach=false" v-if="Result" class="" style="padding: 6px 0 6px 6px;"> 关闭 </view>
					<view v-for="(item,index) in Result" :key="index" class="">
						<view class="titleName" @click="selectName(item.Name)">
							{{item.Name}}
						</view>
					</view>
				</view>
				<li class="df ac sb bgcf pd150 bbf1">
					<view class="">
						<view class="df ac bgcf" hover-class="none" :hover-stop-propagation="false">
							<span class="f14 cf5">*</span>
							<p class="f14 c28">营业执照</p>
						</view>
						<view class="tips cc"> 清晰完整，避免缺边、模糊、反光 </view>
					</view>
					<view class="df ac">
						<view class="img72 pr" v-if="val[1]">
							<image class="img72" referrerPolicy='no-referrer' :src="img_path + val[1]" />
							<image class="img-del" src="https://wx.xtwaixie.com/img/del.png" @tap="onDel(0)" />
						</view>
						<image class="img72" src="https://cdn.xtwaixie.com/uploads/20240705/17201796916687dbebafc4e.png"
							@tap="upImgArr(0)" v-else />
					</view>
				</li>
				<li class="df ac sb h48 bbf1">
					<span class="f14 cf5">*</span>
					<p class="f14 c28">负责人</p>
					<input class="tr f1 ml15 f14 c28" @input="onInput" :data-type="2" :value="val[2]"
						placeholder="请填写负责人" placeholder-class="cc" />
				</li>
				<li class="df ac sb h48 bbf1">
					<span class="f14 cf5">*</span>
					<p class="f14 c28">联系号码</p>
					<input class="tr f1 ml15 f14 c28" @input="onInput" :data-type="3" :value="val[3]" type="number"
						placeholder="请填写联系号码" placeholder-class="cc" />
				</li>
				<li class="df ac sb h48 bbf1" @tap.native="getSetting" :data-type="0">
					<span class="f14 cf5">*</span>
					<p class="f14 c28">工厂定位</p>
					<view class="" style="flex: 1;">
						<input class="tr f1 ml15 f14 c28" :value="area[0] + area[1]+area[2]" placeholder="请选择"
							placeholder-class="cc" disabled />
						<input class="tr f1 ml15 f14 c28" v-if="val[6]" auto-height :data-type="6" :value="val[6]" placeholder="点击定位"
							placeholder-class="cc" disabled />
					</view>
					<image class="img14 ml8" src="https://wx.xtwaixie.com/img/more1.png" />
				</li>
				<li class="df ac sb h48 bbf1">
					<span class="f14 cf5" style="opacity: 0;">*</span>
					<p class="f14 c28">门牌号</p>
					<input class="tr f1 ml15 f14 c28" @input="onInput" :data-type="5" :value="val[5]"
						placeholder="例: 90号3栋" placeholder-class="cc" />
				</li>
				<li class="df ac sb h48 bbf1" @tap.native="changeMachining">
					<span class="f14 cf5">*</span>
					<p class="f14 c28">加工工艺</p>
					<input class="tr f1 ml15 f14 c28" :value="processingTechnology" placeholder="请选择加工工艺"
						placeholder-class="cc" disabled />
					<image class="img14 ml8" src="https://wx.xtwaixie.com/img/more1.png" />
				</li>
			</ul>
			<view class="pd15 mt15" hover-class="none" :hover-stop-propagation="false">
				<button class="sub"
					@tap="onSub">{{info.userInfo.settle_status != 0 ? '重新提交' : '提交'}}</button>
			</view>
			<van-popup :show="show_list[0]" round @close="onClose" position="bottom">
				<van-area :area-list="areaList" @confirm="confirm" @cancel="onClose" columns-num="2" />
			</van-popup>
			<van-popup :show="show_list[1]" round @close="onClose" position="bottom">
				<van-picker :columns="bese_info.machining" show-toolbar @confirm="confirm" value-key="name"
					@cancel="onClose" />
			</van-popup>
			<van-popup :show="show_list[2]" round @close="onClose" position="bottom">
				<van-picker :columns="bese_info.range" show-toolbar @confirm="confirm" value-key="name"
					@cancel="onClose" />
			</van-popup>
		</view>
		<!-- 提交过 -->
		
		<view class="pd15" v-else>
			<view class="pd015 bgcff7 br12">
				<view class="df ac sb pd150 bbf1">
					<p class="f14 c9d">申请时间：{{ info.created_at }}</p>
					<p class="f14 f500 c10" v-if="info.userInfo.settle_status == 1">审核中</p>
					<p class="f14 f500 cf5" v-if="info.userInfo.settle_status == 3">未通过</p>
					<p class="f14 f500 c3a" v-if="info.userInfo.settle_status == 2">已通过</p>
				</view>
				<view class="pd150">
					<h2 class="c28 f18 f600 db">{{ info.company }}</h2>
					<view class="df ac sb">
						<ul class="db f1">
							<li class="db f14 c9d mt5">地区：{{ info.city }}</li>
							<li class="db f14 c9d mt5">负责人：{{ info.person }}</li>
							<!-- <li class="db f14 c9d mt5">地加工范围：{{ info.range_id }}</li> -->
						</ul>
						<image class="img72 br6" @tap.stop.prevent="preview" referrerPolicy='no-referrer'
							:src="info.business_license_show" :data-src="info.business_license_show"></image>
					</view>
				</view>
				<view class="bh mt12" v-if="info.reason">原因：{{ info.reason }}</view>
			</view>
			<view class="pob pd715 mb20">
				<button class="sub" v-if="info.userInfo.settle_status != 1" @tap="onAgain">重新提交</button>
				<button class="sub" v-else @tap="onBack">返回</button>
			</view>
		</view>
		<van-popup :show="showNotice" @close="onClose" custom-style='background:#00000000;'>
			<image src="https://cdn.xtwaixie.com/uploads/20240415/1713170369661ce7c10e9d1.gif" mode="widthFix"
				style="width: 500rpx;padding-bottom: 600rpx;"></image>
		</van-popup>
		<van-popup :show="show" round @close="show = false">
			<p class="df ac jc pd200 mt5 w311">
				<span class="f16 c28 f500 tc"
					style="padding: 0px 30rpx;">重新提交入驻资料，需再次经过平台审核。提交后审核期间，无法使用已入驻供应商功能。</span>
			</p>
			<view class="df ac btf1 h48 mt15">
				<p class="w50 tc f16 c28" @tap.native="show = false">否</p>
				<button class="share-but1 h48 c10" @tap="getSubmit">是</button>
			</view>
		</van-popup>
		<van-popup :show="showTechnology" position="bottom" round @close='showTechnology = false'>
			<view class="" style="width: 100vw;height: 840rpx;border-radius: 0;">
				<technology :lists="bese_info.machining" v-if="showTechnology" @agree="agree"
					:technologyList='machiningList' :maxCount="10" />
			</view>
		</van-popup>
	</view>
</template>
<script>
	import {
		verifyPhoneNumber
	} from "@/utils/util"
	import areaList from '../../utils/area';
	import {
		technology
	} from "@/component/technology.vue"
	const http = require('../../utils/http');
	export default {
		components: {
			technology
		},
		data() {
			return {
				logoUrl: '',
				processingTechnology: '',
				show: false,
				time: -1,
				show_list: [false, false, false],
				areaList: areaList.areaList,
				area: ['', '', ''],
				type_val: '',
				type_id: '',
				material: '',
				material_id: '',
				val: ['', '', '', '', '', ''],
				act: '',
				img_path: '',
				device: [],
				productIndex: -1,
				machiningList:[],
				info: {
					created_at: '',
					status: 0,
					company: '',
					city: '',
					person: '',
					range_id: '',
					business_license_show: '',
					reason: ''
				},
				info_show: false,
				material_val: '',
				showNotice: false,
				bese_info: {
					machining: '',
					range: ''
				},
				latitude: 0,
				longitude: 0,
				isPassed: false,
				SySeach: false,
				Result: [],
				basicResultS: [],
				timer: null,
				gongShow: false,
				isSubmitting: false,
				showTechnology:false,
				callback:null
			};
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		onLoad(options) {
			this.setData({
				area: uni.getStorageSync('top_area') || ['', '', ''],
				img_path: http.img_path
			});
			let token = uni.getStorageSync('token');
			if (token) {
				this.getInfo();
			} else {
				// let is_login = uni.getStorageSync('is_login');
				// if (!is_login) {
				// 	uni.navigateTo({
				// 		url: '/pages/login/login'
				// 	})
				// 	return
				// }
			}
			this.checkLogin();
			this.getBase();
			
		},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady() {},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow() {
			// 验证是否登录，是否有手机号
			this.callback = verifyPhoneNumber()
			if(this.callback) return
			// let token = uni.getStorageSync('token');
			// if(!token){
			// 	uni.showToast({
			// 		title: '您还未登录，请先登录',
			// 		icon: 'none'
			// 	});
			// 	this.callback=setTimeout(() => {
			// 		uni.navigateTo({
			// 			url: '/pages/login/login'
			// 		});
			// 	}, 1000);
			// }
			let is_login = uni.getStorageSync('is_login');
			if (is_login) {
				this.getBase();
			}
		},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide() {},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload() {
			clearTimeout(this.callback)
		},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh() {},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom() {},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage() {},
		methods: {
			gongSFalse() {
				this.gongShow = false;
			},
			gongS() {
				this.gongShow = !this.gongShow;
			},
			changeMachining(productIndex = -1) {
				this.productIndex = productIndex
				this.showTechnology = true
			},
			agree(id, name) {
				this.machiningList = id
				this.processingTechnology = name.join('、')
				this.showTechnology = false
			},
			getSetting() {
				const _this = this
				uni.getSetting({
					success(res) {
						console.log('获取位置');
						if (!res.authSetting['scope.userLocation']) {
							uni.authorize({
								scope: 'scope.userLocation',
								success() {
									_this.chooseLoction()
								},
								fail() {
									uni.showModal({
										title: '请求授权当前位置',
										content: '需要获取您的地理位置，请确认授权',
										success(res) {
											if (res.cancel) {
												//用户拒绝授权
												return uni.showToast({
													title: '拒绝授权获取地址信息',
													icon: 'none'
												});
											} else if (res.confirm) {
												uni.openSetting({
													success(res) {
														console.log(res)
														if (res.authSetting[
																'scope.userLocation']) {
															_this.chooseLoction()
														} else {
															return uni.showToast({
																title: '拒绝授权获取地址信息',
																icon: 'none'
															});
														}
													},
													fail() {
														console.log(111)
														return uni.showToast({
															title: '拒绝授权获取地址信息',
															icon: 'none'
														});
													}
												})
											}
										}
									})
								}
							})
						} else {
							_this.chooseLoction()
						}
					}
				})
			},
			chooseLoction() {
				let that = this
				uni.chooseLocation({
					success: function(res) {
						that.val[4] = res.address
						that.val[6] = res.name
						that.latitude = res.latitude
						that.longitude = res.longitude
						uni.request({
							url: "https://apis.map.qq.com/ws/geocoder/v1/?location=",
							data: {
								key: "XUDBZ-3OVAL-KWEPB-MXU6W-KW4XK-6WBHF",
								location: `${res.latitude},${res.longitude}`
							},
							success: (res) => {
								that.area[0] = res.data.result.address_component.province
								that.area[1] = res.data.result.address_component.city
								that.area[2] = res.data.result.address_component.district
							}
						})
					},
					fail(err) {
						return uni.showToast({
							title: '打开地图失败',
							icon: 'none'
						});
					}
				})
			},
			// 添加设备
			onAdd() {
				this.device.push('');
				this.setData({
					device: this.device
				});
			},
			onDevice(e) {
				let val = e.detail.value;
				let i = e.currentTarget.dataset.type;
				this.setData({
					[`device[${i}]`]: val
				});
			},
			onSub() {
				if (this.info.status == 2) {
					this.time = new Date().getTime()
					this.show = true
				} else {
					this.getSubmit()
				}
			},
			// 提交
			getSubmit() {
				// if(this.time >= 0 && new Date().getTime() - this.time < 3000){
				// 	return
				// }
				if (!this.val[0]) {
					uni.showToast({
						title: '请输入企业名称',
						icon: 'none'
					});
					return false;
				}
				if (!this.val[1]) {
					uni.showToast({
						title: '请上传营业执照',
						icon: 'none'
					});
					return false;
				}
				if (!this.val[2]) {
					uni.showToast({
						title: '请输入负责人',
						icon: 'none'
					});
					return false;
				}
				if (!this.val[3]) {
					uni.showToast({
						title: '请输入联系号码',
						icon: 'none'
					});
					return false;
				}
				if (!this.val[6]) {
					uni.showToast({
						title: '请设置地址',
						icon: 'none'
					});
					return false;
				}
				if (this.isPassed == false) {
					uni.showToast({
						title: '未通过工商认证',
						icon: 'none'
					});
					return false;
				}
				if (this.SySeach == true) {
					uni.showToast({
						title: '请选择企业公司',
						icon: 'none'
					});
					return false;
				}
				if (!this.processingTechnology) {
					uni.showToast({
						title: '请选择加工工艺',
						icon: 'none'
					});
					return false;
				}
				// if (!this.val[5]) {
				// 	uni.showToast({
				// 		title: '请填写门牌号',
				// 		icon: 'none'
				// 	});
				// 	return false;
				// }
				let phoneReg = /^1\d{10}$/;
				if (!phoneReg.test(this.val[3])) {
					uni.showToast({
						title: '填写手机号位数不对',
						icon: 'none'
					})
					return
				}
				this.show = false
				let ifFirstPopup = uni.getStorageSync('ifFirstPopup')
				if (!ifFirstPopup) {
					this.showNotice = true
					uni.setStorageSync('ifFirstPopup', true)
				}
				const self = this
				uni.requestSubscribeMessage({
					tmplIds: ['92OIalTSykZRY_WjSV0KahZNSwxMLTE4RhnC3lzAIgw'],
					complete() {
						self.showNotice = false
						self.submit()
					}
				})
			},
			submit() {
				if (this.isSubmitting) {
					return;
				}
				this.isSubmitting = true;
				let resultS = this.isPassed ? 1 : 0;
				let data = {
					company: this.val[0],
					business_license: this.val[1],
					province: this.area[0],
					city: this.area[1],
					county: this.area[2],
					machining_id:this.machiningList,
					person: this.val[2],
					mobile: this.val[3],
					address_title: this.val[6],
					address: this.val[4],
					house_number: this.val[5],
					device: this.device.toString(),
					latitude: this.latitude,
					longitude: this.longitude,
					state: resultS,
					// machining_id: this.machiningList,
				};
				http.request('user/settled', data).then((res) => {
					uni.reLaunch({
						url: '/pages/editSuccess/editSuccess'
					});
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
				});
			},
			// 弹框显示
			onArea(e, _dataset) {
				/* ---处理dataset begin--- */
				this.handleDataset(e, _dataset);
				/* ---处理dataset end--- */
				let i = e.currentTarget.dataset.type;
				let status = this.show_list[i];
				this.setData({
					[`show_list[${i}]`]: !status,
					act: i
				});
			},
			//  弹框关闭
			onClose() {
				this.showNotice = false
				this.setData({
					show_list: [false, false, false]
				});
			},
			//  弹框确认点击
			confirm(e) {
				let act = this.act;
				switch (act) {
					case 0:
						this.setData({
							[`area[0]`]: e.detail.values[0].name,
							[`area[1]`]: e.detail.values[1].name,
							[`area[2]`]: e.detail.values[2].name,
						});
						this.onClose();
						break;
					case 1:
						this.setData({
							material_val: e.detail.value.name,
							material_id: e.detail.value.id
						});
						this.onClose();
						break;
					case 2:
						this.setData({
							type_val: e.detail.value.name,
							type_id: e.detail.value.id
						});
						this.onClose();
						break;
				}
			},
			selectName(name) {
				this.SySeach = false
				this.isPassed = true
				this.setData({
					[`val[${0}]`]: name
				});
				// http.request('user/getbasic', {
				// 	name: name
				// }).then((res) => {
				// 	console.log(res.Result);
				// }).catch((err) => {
				// 	console.log(err.Result);
				// 	this.basicResultS=err.Result
				// });
			},
			// input显示
			onInput(e) {
				console.log(e.detail.value)
				let val = e.detail.value;
				let i = e.currentTarget.dataset.type;
				this.setData({
					[`val[${i}]`]: val
				});
			},
			onInputYY(e) {
				console.log(e.detail.value)
				if (this.SySeach = true) {
					this.isPassed = false;
				}
				const val = e.detail.value;
				const i = e.currentTarget.dataset.type;
				this.setData({
					[`val[${i}]`]: val,
				});
				// 清除之前的定时器
				if (this.timer) {
					clearTimeout(this.timer);
				}
				// 设置新的定时器，延迟500毫秒后执行
				this.timer = setTimeout(() => {
					if (val.length >= 4) {
						this.SySeach = true;
						http.request("user/qichacha", {
							name: val
						}).then((res) => {
							console.log(res.data.Result.Data);
							this.Result = res.Result.Data;
							console.log(res.Result.Data);
						}).catch((err) => {
							this.Result = err.Result.Data;
							console.log(err.Result.Data);
						});
					} else {
						this.isPassed = false;
						this.SySeach = false
					}
				}, 500); // 延迟时间可以根据需要调整
			},
			// 获取配置
			getBase() {
				http.request('base/configData').then((res) => {
					this.setData({
						bese_info: res.data
					});
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
				});
			},
			//  上传照片
			upImgArr(index) {
				let that = this;
				uni.chooseImage({
					count: 1,
					sizeType: 'compressed',
					success(res) {
						http.upLoadFile('base/uploadImg', 'image', res.tempFilePaths[0], {}).then((res) => {
							if (index === 0) {
								that.setData({
									[`val[1]`]: res.data.url
								});
							} else {
								that.logoUrl = res.data.url
							}
						}).catch((res) => {
							uni.showToast({
								title: res.message,
								icon: 'none'
							});
						});
					},
					fail() {}
				});
			},
			// 删除图片
			onDel(e) {
				if (e === 0) {
					this.setData({
						[`val[1]`]: ''
					});
				} else {
					this.logoUrl = ''
				}
			},
			// 预览
			preview(event) {
				let currentUrl = event.target.dataset.src;
				uni.previewImage({
					current: currentUrl,
					// 当前显示图片的http链接
					urls: [currentUrl] // 需要预览的图片http链接列表
				});
			},
			getInfo() {
				http.request('user/settledInfo').then((res) => {
					console.log(res.data.info.basic);
					let basicTG = res.data.info.basic
					if (basicTG) {
						this.isPassed = true
					}
					this.latitude=res.data.info.latitude
					this.longitude=res.data.info.longitude
					if (res.data.info.company) {
						const status = res.data.info.userInfo.settle_status
						if (status == 1 || status == 3) {
							this.info_show = true
						} else {
							this.info_show = false
						}
						
						this.setData({
							[`val[0]`]: res.data.info.company,
							[`val[1]`]: res.data.info.business_license,
							[`val[2]`]: res.data.info.person,
							[`val[3]`]: res.data.info.mobile,
							[`val[6]`]: res.data.info.address_title,
							[`val[4]`]: res.data.info.address,
							[`val[5]`]: res.data.info.house_number,
							['area[0]']: res.data.info.province,
							['area[1]']: res.data.info.city,
							['area[2]']: res.data.info.county,
							device: res.data.info.device || []
						});
						
					}
					this.setData({
						info: res.data.info
					});
					this.processingTechnology = res.data.info.machining.join('、')
					this.machiningList = JSON.parse(res.data.info.machining_id) || []
				}).catch((res) => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					});
				});
			},
			onAgain() {
				this.setData({
					info_show: false
				});
			},
			// 返回
			onBack() {
				uni.redirectTo({
					url: "/pages/my/my"
				})
				// uni.navigateBack({
				// 	delta: 2
				// });
			},
			checkLogin() {
				const token = uni.getStorageSync('token')
				if (!token) {
					uni.showToast({
						title: '您还未登录',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}, 0)
				}
			},
		},
		
	};
</script>
<style>
	@import '../settled/settled.css';

	.tips {
		margin-top: 4rpx;
		font-size: 28rpx;
	}

	.share-but1 {
		width: 50% !important;
		font-size: 32rpx;
		padding: 0 !important;
		margin: 0 !important;
		font-weight: 400;
		text-align: center;
		color: #283043;
		line-height: 96rpx;
		border-left: 1rpx solid #ebedf0;
	}

	.passed {
		position: relative;
		width: 50rpx;
		height: 50rpx;
		margin-left: 15rpx;
	}

	.pd_tab {
		position: absolute;
		bottom: -60rpx;
		right: 0rpx;
		width: 160rpx;
		height: 68rpx;
		box-sizing: border-box;
		color: #fff;
		z-index: 22;
	}

	.pd_tab>image {
		width: 100%;
		height: 100%;
	}

	.active_pd {
		background-color: #0E932E !important;
	}

	.search-D {
		position: absolute;
		/* top: 120%; */
		background: white;
		width: 100%;
		z-index: 12;
		box-shadow: 0 3px 5px 0 rgba(0, 0, 0, .1);
		border-radius: 0 0 10px 10px;
		font-size: 14px;
		display: flex;
		flex-direction: column;
	}

	.titleName {
		line-height: 30px;
		padding: 6px 0 6px 6px;
		border-bottom: 1px solid gainsboro;
	}
</style>